#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

//创建在静态区上
//class Singleton{
//public:
//    static Singleton & getInstance(){
//        cout << &_s << endl;
//        return _s;
//    }
//private:
//    Singleton(){}
//    Singleton(const Singleton & rhs) = delete;
//    Singleton & operator=(const Singleton & rhs) = delete;
//    static Singleton _s;
//};
//Singleton Singleton::_s;
//
//void test(){
//    Singleton::getInstance();
//    Singleton::getInstance();
//}

//创建在堆区
class Singleton{
public:
    static Singleton * getInstance(){
        if(_ps == nullptr){
            _ps = new Singleton();
        }
        return _ps;
    }

    void destory(){
        if(_ps){
            delete _ps;
            _ps = nullptr;
        }
    }
private:
    Singleton(){}
    ~Singleton() {}
    Singleton(const Singleton & rhs) = delete;
    Singleton & operator=(const Singleton & rhs) = delete;
    static Singleton * _ps;
};
Singleton * Singleton::_ps = nullptr;

void test(){
    Singleton *s1 = Singleton ::getInstance();
    Singleton *s2 = Singleton ::getInstance();

    cout << s1 << endl;
    cout << s2 << endl;

    Singleton::getInstance()->destory();
}

int main()
{
    test();
    return 0;
}

